<?php
namespace BoxApi\Controller;

use Think\Controller;

class SubjectController extends CommonController {
//课程表
	function lesson() {
		$subject_id = I('subject_id');
		$sections = BoxModel("edu_sections")->get(array('sectionsubjectid' => $subject_id, 'status' => 1, 'is_show' => 1), "", "order_by");
		$model = BoxModel("edu_knows");
		foreach ($sections as $k => $one) {
			$sections[$k]['knows'] = $model->get(array('knowssectionid' => $one['sectionid']), "", "order_by");
			$modelKnowsUser = BoxModel("edu_knows_user");
			foreach ($sections[$k]['knows'] as $k1 => $one) {
				if ($this->user_info['user_id']) {
					$sections[$k]['knows'][$k1]['history'] = $modelKnowsUser->where(array('user_id' => $this->user_info['user_id'], 'knowsid' => $one['knowsid']))->find();
				}
			}
		}
		exit(json_encode($sections));
	}
	//课程评论
	function subject_comment() {
		$subject_id = I('subject_id');
		$where['type'] = "orderComment";
		$where['is_show'] = 1;
		$where['pid'] = $subject_id;
		$subject_comment = BoxModel("sns_topic")->get($where, 30, "lastupdate desc");
		//$this->show_page($this->subject_comment['pager']);
		exit(json_encode($subject_comment));

	}

	//我的课程
	function my_subject() {
		$user_info = Logic("BoxEdu", "User")->info($this->user_id);
		$mySubjectids = $user_info['subjectids_info'];
		$learning_subject = array();
		$over_subject = array();
		foreach ($mySubjectids as $k => $one) {
			$subs[] = $one['subjectid'];
			if ($one['end_date'] < date("Y-m-d")) {
				$mySubjectids[$k]['type'] = "over";
				$over_subject[] = $one;
			} else {
				$learning_subject[] = $one;
			}
		}
		//继续开通
		$sub_exp = trim(implode(",", $subs));
		if ($sub_exp) {
			$data = BoxModel("EduSubject")->get("is_show=1  and subjectid not in($sub_exp)", 5);
		} else {
			$data = BoxModel("EduSubject")->get("is_show=1 ", 5);
		}

		$data = array(
			'more' => $data,
			'my' => array_values($mySubjectids),
		);
		exit(json_encode($data));

	}
	//答疑列表
	public function topic() {
		$where = array(
			'is_show' => 1,
			'type' => 'eduAsk',
		);
		$limit = I('limit', "10", "intval");
		//课程
		if ($pid = I('pid', "", "intval")) {
			$where['pid'] = $pid;
		}
		//章节
		if ($pid2 = I('pid2', "", "intval")) {
			$where['pid2'] = $pid2;
		}
		//课时
		if ($pid3 = I('pid3', "", "intval")) {
			$where['pid3'] = $pid3;
		}
		//题
		if ($pid4 = I('pid4', "", "intval")) {
			$where['pid4'] = $pid4;
		}
		$this->type = I("type", "0", "intval");
		$typeWhere = array(
			'1' => array('is_best' => 0),
			'2' => array('is_best' => 1),
			'3' => array('user_id' => intval($this->user_id)),
		);
		if ($addWhere = $typeWhere[$this->type]) {
			$where = array_merge($where, $addWhere);
			//dump($where);
		}

		$data = BoxModel("sns_topic")->get($where, $limit, "lastupdate desc");
		exit(json_encode($data));
	}

	//保存笔记
	public function save_note() {
		$row = array(
			'notes' => I('notes'),
			'knowsid' => intval(I('knowsid')),
			'user_id' => $this->user_id,
			'updatetime' => date("Y-m-d H:i:s"),
		);
		$model = BoxModel("edu_subject_notes");
		$where = array('user_id' => $this->user_id, 'knowsid' => $row['knowsid']);
		$f = $model->where($where)->find();
		if ($f) {
			$id = $model->where($where)->save($row);
		} else {
			$id = $model->add($row);
		}
		if ($id !== false) {
			exit(json_encode(array('error' => 0, 'msg' => '保存成功')));
		} else {
			exit(json_encode(array('error' => 1, 'msg' => '操作失败')));
		}

	}
	//动态
	public function timeline() {
		$where = array();
		$limit = I('limit', "10", "intval");
		if ($subjectid = I('subjectid', "0", "intval")) {
			$where['subjectid'] = $subjectid;
		}
		if ($user_id = I('user_id', "0", "intval")) {
			$where['user_id'] = $this->user_id;
		}

		if ($type = I('type')) {
			$where['type'] = $type;
		}

		if ($public = I('public', "0", "intval")) {
			$where['public'] = $public;
		}

		//	dump($where);
		$data = BoxModel("SystemLog")->get($where, $limit);
		$model = BoxModel("system_log_template");
		$userModel = BoxModel("system_member");
		$from = dict("drive_from");
		foreach ($data['list'] as $k => $one) {
			$tplData = $model->where(array('event' => $one['event']))->find();
			$one['icon'] = $tplData['icon'] ? $tplData['icon'] : 'glyphicon glyphicon-gift';
			$one['from_str'] = $from[$one['from']];
			$one['user'] = $userModel->load($one['user_id']);
			$one['time_tran'] = time_tran(strtotime($one['add_date']));
			$one['remark'] = strip_tags($one['remark']);
			$data['list'][$k] = $one;
		}
		$data['list'] = (array) $data['list'];
		exit(json_encode($data));

	}

}